//! 客户端错误定义

use thiserror::Error;

#[derive(Error, Debug)]
pub enum ClientError {
    #[error("Network error: {0}")]
    NetworkError(String),
    
    #[error("Server error: {0}")]
    ServerError(String),
    
    #[error("Timeout error: {0}")]
    TimeoutError(String),
    
    #[error("Authentication error: {0}")]
    AuthError(String),
    
    #[error("Serialization error: {0}")]
    SerializationError(String),
    
    #[error("Configuration error: {0}")]
    ConfigError(String),
    
    #[error("Service not found: {0}")]
    ServiceNotFound(String),
    
    #[error("Config not found: {0}")]
    ConfigNotFound(String),
    
    #[error("Invalid response: {0}")]
    InvalidResponse(String),
    
    #[error("Retry exhausted: {0}")]
    RetryExhausted(String),
    
    #[error("Client already closed")]
    ClientClosed,
}

impl From<reqwest::Error> for ClientError {
    fn from(err: reqwest::Error) -> Self {
        ClientError::NetworkError(err.to_string())
    }
}

impl From<serde_json::Error> for ClientError {
    fn from(err: serde_json::Error) -> Self {
        ClientError::SerializationError(err.to_string())
    }
}

impl From<url::ParseError> for ClientError {
    fn from(err: url::ParseError) -> Self {
        ClientError::ConfigError(err.to_string())
    }
}

impl From<tonic::transport::Error> for ClientError {
    fn from(err: tonic::transport::Error) -> Self {
        ClientError::NetworkError(err.to_string())
    }
}

impl From<tonic::Status> for ClientError {
    fn from(err: tonic::Status) -> Self {
        ClientError::ServerError(err.to_string())
    }
}

pub type ClientResult<T> = Result<T, ClientError>;